package cn.iocoder.yudao.module.hs.controller.admin.materialbox;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.materialbox.vo.MaterialBoxPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.materialbox.vo.MaterialBoxRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.materialbox.vo.MaterialBoxSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.materialbox.MaterialBoxDO;
import cn.iocoder.yudao.module.hs.service.materialbox.MaterialBoxService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物料箱信息")
@RestController
@RequestMapping("/hs/material-box")
@Validated
public class MaterialBoxController {

    @Resource
    private MaterialBoxService materialBoxService;

    @PostMapping("/create")
    @Operation(summary = "创建物料箱信息")
    @PreAuthorize("@ss.hasPermission('hs:material-box:create')")
    public CommonResult<Long> createMaterialBox(@Valid @RequestBody MaterialBoxSaveReqVO createReqVO) {
        return success(materialBoxService.createMaterialBox(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料箱信息")
    @PreAuthorize("@ss.hasPermission('hs:material-box:update')")
    public CommonResult<Boolean> updateMaterialBox(@Valid @RequestBody MaterialBoxSaveReqVO updateReqVO) {
        materialBoxService.updateMaterialBox(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料箱信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:material-box:delete')")
    public CommonResult<Boolean> deleteMaterialBox(@RequestParam("id") Long id) {
        materialBoxService.deleteMaterialBox(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料箱信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:material-box:query')")
    public CommonResult<MaterialBoxRespVO> getMaterialBox(@RequestParam("id") Long id) {
        MaterialBoxDO materialBox = materialBoxService.getMaterialBox(id);
        return success(BeanUtils.toBean(materialBox, MaterialBoxRespVO.class));
    }

    @GetMapping("/get-box")
    @Operation(summary = "获得物料箱信息")
    @Parameter(name = "boxCode", description = "编号")
    @PreAuthorize("@ss.hasPermission('hs:material-box:query')")
    public CommonResult<MaterialBoxRespVO> getMaterialBox(@RequestParam("boxCode") String boxCode) {
        MaterialBoxDO materialBox = materialBoxService.getMaterialBox(boxCode);
        return success(BeanUtils.toBean(materialBox, MaterialBoxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料箱信息分页")
    @PreAuthorize("@ss.hasPermission('hs:material-box:query')")
    public CommonResult<PageResult<MaterialBoxRespVO>> getMaterialBoxPage(@Valid MaterialBoxPageReqVO pageReqVO) {
        PageResult<MaterialBoxDO> pageResult = materialBoxService.getMaterialBoxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialBoxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料箱信息 Excel")
    @PreAuthorize("@ss.hasPermission('hs:material-box:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialBoxExcel(@Valid MaterialBoxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialBoxDO> list = materialBoxService.getMaterialBoxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料箱信息.xls", "数据", MaterialBoxRespVO.class,
                        BeanUtils.toBean(list, MaterialBoxRespVO.class));
    }

    @GetMapping("/check-box-in-tray")
    @Operation(summary = "判断当前物料箱是否在该托盘中")
    @Parameter(name = "boxCode", description = "物料箱码")
    @Parameter(name = "trayCode", description = "托盘码")
    @PreAuthorize("@ss.hasPermission('hs:material-box:query')")
    public CommonResult<Boolean> checkBoxInTray(@RequestParam("boxCode") String boxCode, @RequestParam("trayCode") String trayCode) {
        return success(materialBoxService.checkBoxInTray(boxCode, trayCode));
    }
}